﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using ATMConst;
using Package;

using ATMCommonExceptions;

namespace ATMCableCloud
{
    class Program
    {
		static int Main(string[] args)
        {

				ConnectedAtmNodes allNodes = ConnectedAtmNodes.getInstance();
				/*if (args.Length  == 0 || args.Length > 2)
				{
					Console.WriteLine("Using:\nprogram_name settings_filename port_number");
					return 1;
				}
				if (args.Length == 2)*/
				{
					try
					{
						InputFileParser.getInstance().parseFile("test.txt");
						Console.WriteLine("Loaded network setting from {0}", "test.txt");
						int port_number = (Int32.TryParse(args[1], out port_number) ? Int32.Parse("50001") : Const.DEFAULT_PORT_NUMBER);
						MainTCPServer ServerTCP = MainTCPServer.getInstance();
						ThreadStart TCP_server_function = new ThreadStart(serverTCP);
						Thread TCP_server_thread = new Thread(TCP_server_function);
						TCP_server_thread.IsBackground = true;
						TCP_server_thread.Start();
						Console.WriteLine("Write q to exit program");
						while (true)
						{
							string s = Console.ReadLine();
							if (s.Equals("q"))
								return 0;
						}
					}
					catch (SyntaxErrorInFile e)
					{
						Console.WriteLine("Fatal Error during loading settings.\n {0}", e);

					}
				}
				return 1;

        }
		static void serverTCP()
		{
			MainTCPServer.getInstance().start();
		}
    }
}
